/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package company.folder.beans;

import facades.CandidateLanguageInfoFacade;
import facades.CandidateProfessionalInfoFacade;
import entities.Candidate;
import entities.CandidateLanguageInfo;
import entities.CandidateProfessionalInfo;
import entities.Company;
import entities.CompanyAdquiredProfile;
import entities.CompanyAdquiredProfileFolder;
import entities.Notification;
import entities.NotificationType;
import facades.NotificationFacade;
import facades.NotificationTypeFacade;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.event.Event;
import javax.enterprise.event.Observes;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import qualifiers.ChosenEntityEvent;
import qualifiers.LoggedIn;
import qualifiers.NotificationEmail;
import qualifiers.ThrowNotification;

/**
 *
 * @author damfercer
 */
@Named
@SessionScoped
public class ViewFolderCandidateBean implements Serializable {
    private CompanyAdquiredProfile adquiredProfile;
    private CandidateLanguageInfo selectedLanguageInfo;
    private List<String> imageList;
    @Inject
    private CandidateLanguageInfoFacade languageEJB;
    @Inject
    private CandidateProfessionalInfoFacade professionalEJB;
    @Inject
    private NotificationFacade notificationFacade;
    @Inject
    private NotificationTypeFacade notificationTypeFacade;
    @Inject
    @ThrowNotification
    Event<Notification> throwNotificationEvent;
    @Inject
    @LoggedIn
    private Company company;
    @Inject
    @NotificationEmail
    private Event<Notification> notificationEmail;
    /**
     * Creates a new instance of ViewCandidateBean
     */
    public ViewFolderCandidateBean() {
    }
    
    @PostConstruct
    public void init(){
        imageList=new ArrayList<String>();
    }
    
    public void onSelectedAdquiredProfile(@Observes @ChosenEntityEvent CompanyAdquiredProfile profile){
        this.adquiredProfile=profile;
        if(this.adquiredProfile.getCandidate().getBustColor()!=null){
            imageList.add(this.adquiredProfile.getCandidate().getBustColor());
        }
        if(this.adquiredProfile.getCandidate().getBustBlackWhite()!=null){
            imageList.add(this.adquiredProfile.getCandidate().getBustBlackWhite());
        }
        if(this.adquiredProfile.getCandidate().getBodyFormal()!=null){
            imageList.add(this.adquiredProfile.getCandidate().getBodyFormal());
        }
        if(this.adquiredProfile.getCandidate().getBodyUnformal()!=null){
            imageList.add(this.adquiredProfile.getCandidate().getBodyUnformal());
        }
    }
    
    public void onSelectedAdquiredProfile(@Observes @ChosenEntityEvent CompanyAdquiredProfileFolder profileFolder){
        this.adquiredProfile=profileFolder.getCompanyAdquiredProfile();
        if(this.adquiredProfile.getCandidate().getBustColor()!=null){
            imageList.add(this.adquiredProfile.getCandidate().getBustColor());
        }
        if(this.adquiredProfile.getCandidate().getBustBlackWhite()!=null){
            imageList.add(this.adquiredProfile.getCandidate().getBustBlackWhite());
        }
        if(this.adquiredProfile.getCandidate().getBodyFormal()!=null){
            imageList.add(this.adquiredProfile.getCandidate().getBodyFormal());
        }
        if(this.adquiredProfile.getCandidate().getBodyUnformal()!=null){
            imageList.add(this.adquiredProfile.getCandidate().getBodyUnformal());
        }
    }
    
    public List<String> getImageList(){
        return imageList;
    }
    
    public String getIdFromCandidate(){
        String res=null;
        CandidateLanguageInfo best=languageEJB.getBestCandidateLanguageInfoVideo(this.adquiredProfile.getCandidate());
        if(best!=null){
            String[] array=best.getVideo().split("\\.");
            res=array[0];
        }
        return res;
    }
    
    public CandidateLanguageInfo getVideoIndex(){
        return languageEJB.getBestCandidateLanguageInfoVideo(this.adquiredProfile.getCandidate());
    }
    
    public String getProfessionalExperienceString(){
        String res="";
        List<CandidateProfessionalInfo> experiences=this.adquiredProfile.getCandidate().getCandidateProfessionalInfoList();
        if(experiences!=null && !experiences.isEmpty()){
            int size=experiences.size();
            if(size==1){
                res+=" "+experiences.get(0).getSector().getDescription();
            }
            else{
                int i=0;
                for(CandidateProfessionalInfo prof: experiences){
                    if(i==size-1){
                        FacesContext fc=FacesContext.getCurrentInstance();
                        Locale locale=fc.getViewRoot().getLocale();
                        ResourceBundle rb = ResourceBundle.getBundle("messages", locale);
                        String and=rb.getString("And");
                        res+=" "+and+" "+prof.getSector().getDescription();
                    }
                    else if(i==0){
                        res+=" "+prof.getSector().getDescription();
                    }
                    else{
                        res+=", "+prof.getSector().getDescription();
                    }
                    i++;
                }
            }
        }
        return res;
    }
    
    public Candidate getCandidate(){
        return this.adquiredProfile.getCandidate();
    }
    
    public String getLanguageString(){
        String res="";
        List<CandidateLanguageInfo> languages=this.adquiredProfile.getCandidate().getCandidateLanguageInfoList();
        if(languages!=null && !languages.isEmpty()){
            int size=languages.size();
            if(size==1){
                res+=" "+languages.get(0).getLanguage().getDescription();
            }
            else{
                int i=0;
                for(CandidateLanguageInfo prof: languages){
                    if(i==size-1){
                        FacesContext fc=FacesContext.getCurrentInstance();
                        Locale locale=fc.getViewRoot().getLocale();
                        ResourceBundle rb = ResourceBundle.getBundle("messages", locale);
                        String and=rb.getString("And");
                        res+=" "+and+" "+prof.getLanguage().getDescription();
                    }
                    else if(i==0){
                        res+=" "+prof.getLanguage().getDescription();
                    }
                    else{
                        res+=", "+prof.getLanguage().getDescription();
                    }
                    i++;
                }
            }
        }
        return res;
    }
    
    public String getIdFromCandidateLanguageInfo(CandidateLanguageInfo cli){
        String res=null;
        if(cli!=null){
            String[] array=cli.getVideo().split("\\.");
            res=array[0];
        }
        return res;
    }
    
    public CandidateProfessionalInfo getBestProfessionalInfoFromCandidate(){
        return professionalEJB.getBestCandidateProfessionalInfo(this.adquiredProfile.getCandidate());
    }
    
    public Integer dateDifference(Date compare){
        Date now=new Date();
        long diff=now.getTime()-compare.getTime();
        long days=diff / 1000 / 60 / 60 / 24 / 365;
        return (int)days;
    }
    
    public boolean candidateHasVideo(){
        boolean res = false;
        for(CandidateLanguageInfo ca: getCandidate().getCandidateLanguageInfoList()){
            if(ca.getVideo()!=null && !ca.getVideo().equals("0")){
                res=true;
                break;
            }
        }
        return res;
    }
    
    public boolean companyRequestedVideo(){
    return notificationFacade.isRequestedVideo(getCandidate(), company);
    }
    
    public void requiredNewVideo(){
        StringBuilder mensajeNotificacion=new StringBuilder();
        if(company.getCompanyConfiguration().getShowName()){
            mensajeNotificacion.append(company.getName());
        }
        else{
            mensajeNotificacion.append(getMessages().getString("NoNameCompany"));
        }
        mensajeNotificacion.append(" ");
        mensajeNotificacion.append(getMessages().getString("RequiredYouAVideo"));
        mensajeNotificacion.append(" ");
        mensajeNotificacion.append(convertDateToString(new Date()));
        
        Notification notification=new Notification();
        notification.setIsNotificated(false);
        notification.setDescription(mensajeNotificacion.toString());
        notification.setDate(new Date());
        notification.setUser(getCandidate().getUser());
        notification.setUserSender(company.getIdUser());
        NotificationType notificationType= notificationTypeFacade.find(2);
        notification.setNotificationType(notificationType);
        notificationFacade.create(notification);
        throwNotificationEvent.fire(notification);
        if(notification.getUser().getCandidate().getCandidateConfiguration()==null){
            notificationEmail.fire(notification);
        }
        else if(notification.getUser().getCandidate().getCandidateConfiguration().getEmailVideo()){
            notificationEmail.fire(notification);
        }

    }

    /**
     * @return the selectedLanguageInfo
     */
    public CandidateLanguageInfo getSelectedLanguageInfo() {
        return selectedLanguageInfo;
    }

    /**
     * @param selectedLanguageInfo the selectedLanguageInfo to set
     */
    public void setSelectedLanguageInfo(CandidateLanguageInfo selectedLanguageInfo) {
        this.selectedLanguageInfo = selectedLanguageInfo;
    }

    /**
     * @return the adquiredProfile
     */
    public CompanyAdquiredProfile getAdquiredProfile() {
        return adquiredProfile;
    }

    /**
     * @param adquiredProfile the adquiredProfile to set
     */
    public void setAdquiredProfile(CompanyAdquiredProfile adquiredProfile) {
        this.adquiredProfile = adquiredProfile;
    }
    
        public ResourceBundle getMessages(){
        FacesContext context = FacesContext.getCurrentInstance();
        Locale locale=context.getViewRoot().getLocale();
        ResourceBundle rb = ResourceBundle.getBundle("messages", locale);
        return rb;
    }
    
    public String convertDateToString(Date date){
        SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy - HH:mm");
        String dateString = sdf.format(date);
        return dateString;
    }
}
